package com.qf.mybatis01.test;

import com.github.pagehelper.PageHelper;
import com.qf.mybatis01.dao.BookDao;
import com.qf.mybatis01.entity.Book;
import com.qf.mybatis01.utils.MybatisUtils;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class Print {
    public static void main(String[] args) {
        PrintWriter printWriter=null;
        BookDao mapper=null;
        int pageSize=2000;
        List<Book> allBook=null;
        try {
            printWriter=new PrintWriter(new FileWriter("src/main/resources/books.txt"));
            BookDao mapper1 = MybatisUtils.getMapper(BookDao.class);
            Integer totalBook = mapper1.getAllBookCount();
            int count=0;
            if(totalBook%pageSize==0){
                count=totalBook/pageSize;
            }else{
                count=totalBook/pageSize+1;
            }
            System.out.println(count);
            int i=1;
            List<Book> temp=new ArrayList<>();
            while (i <= count){
                PageHelper.startPage(i,pageSize);
                mapper = MybatisUtils.getMapper(BookDao.class);
                allBook= mapper.getBook();
                int size=0;
                for (Book book : allBook) {
                    Integer id=book.getId();
                    String name=book.getName();
                    String author=book.getAuthor();
                    String publish=book.getPublish();
                    Integer sort = book.getSort();
                    printWriter.println(id+","+name+","+author+","+publish+","+sort);
                    System.out.println(book);
                }
                i++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            printWriter.close();
        }
    }
}

